#include "TextureCache.h"

#include <SFML/Graphics.hpp>

#include <stdio.h>


TextureCache::TextureCache()
: Singleton<TextureCache>()
{
}

TextureCache::~TextureCache()
{
    for(TCache::iterator it = m_cache.begin(); it != m_cache.end(); ++it)
    {
        delete it->second;
    }
}

sf::Texture* TextureCache::LoadTexture(const char* fileName)
{
    if(m_cache.count(fileName) == 0)
    {
        sf::Texture* texture = new sf::Texture();
        if(!texture)
        {
            return NULL;
        }

        if(!texture->loadFromFile(fileName))
        {
            printf("Failed to load texture %s\n", fileName);
            return NULL;
        }

        texture->setSmooth(false);
        texture->setRepeated(false);

        m_cache[fileName] = texture;
    }

    return m_cache[fileName];
}
